
/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：用户银行卡                                         
*│　作    者：zzc                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-01-04 10:01:02                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Services                                  
*│　类    名：ManagerDataService                           
*└──────────────────────────────────────────────────────────────┘
*/
using AutoMapper;
using BestTrader.Cms.Core.Extensions;
using BestTrader.Cms.IRepository;
using BestTrader.Cms.IServices;
using BestTrader.Cms.Models;
using BestTrader.Cms.ViewModels;
using Microsoft.Extensions.Logging;
using System;


namespace BestTrader.Cms.Services
{
    public class ManagerBankCardService : IManagerBankCardService
    {
        private readonly IManagerBankCardRepository _repository;
        private readonly IManagerRepository _managerRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<ManagerBankCardService> _logger;


        public ManagerBankCardService(IManagerBankCardRepository repository, IManagerRepository managerRepository, IMapper mapper, ILogger<ManagerBankCardService> logger)
        {
            _repository = repository;
            _managerRepository = managerRepository;
            _mapper = mapper;
            _logger = logger;

        }

        /// <summary>
        /// 修改
        /// </summary>
        public BaseResult ModifyBankCard(ManagerModifyBankCardModel item, String UserId)
        {
            var result = new BaseResult();
            var now = DateTime.Now;
            var _userId = 0;

            if (int.TryParse(UserId, out _userId))
            {

                var managerBankCard = _repository.GetManagerBankCardByManagerId(_userId);

                if (managerBankCard != null)
                {
                    _logger.LogWarning($"用户修改了充值中心银行卡：UserId:{_userId},oldCard:{managerBankCard.BankCard} ");

                    managerBankCard.BankCity = item.BankCity;
                    managerBankCard.BankType = item.BankType;
                    managerBankCard.BankCard = item.BankCard;
                    managerBankCard.BankProvince = item.BankProvince;
                    managerBankCard.ModifyTime = now;
                    managerBankCard.ModifyManagerId = _userId;
                    //managerBankCard.ManagerId = 222;
                    if (_repository.Update(managerBankCard) > 0)
                    {

                        result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                    }
                    else
                    {
                        result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                        result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                    }
                }
                else if (item != null)
                {
                    item.ManagerId = _userId;
                    result = AddBankCard(item);

                }
                else
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonFailNoDataCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonFailNoDataMsg;
                }
            }

            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public BaseResult AddBankCard(ManagerModifyBankCardModel item)
        {
            var result = new BaseResult();
            var now = DateTime.Now;
            ManagerBankCard entity;
            var existData = _repository.GetManagerBankCardByManagerId(item.ManagerId);

            ///检查是否存在
            if (existData == null)
            {
                entity = _mapper.Map<ManagerBankCard>(item);
                entity.AddTime = now;
                entity.AddManagerId = item.ManagerId;

                if (_repository.Insert(entity) > 0)
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                }
                else
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                }
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonFailNoDataCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonFailNoDataMsg;
            }

            return result;
        }



        public TableDataResult<ManagerBankCard> GetManagerBankCardByManagerId(string UserId)
        {
            var result = new TableDataResult<ManagerBankCard>();
            int _userId = 0;
            if (int.TryParse(UserId, out _userId))
            {
                var data = _repository.GetManagerBankCardByManagerId(_userId);
                if (data != null)
                {
                    result.Data = data;
                    result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
                }
                else
                {
                    result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                    result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg;
                }
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonFailNoDataCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonFailNoDataMsg;
            }
            return result;
        }
    }
}